package com.adt.studioms.api;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.adt.studioms.om.Task;
import com.asao.baseware.core.api.hibernate.HibernateDAOAwareAPI;

public class TaskAPI extends HibernateDAOAwareAPI<Task> {

    protected Class<Task> getEntityClass() {
        return Task.class;
    }
    
    public Task createTask(String code, String name) {
        Task task = new Task();
        task.setCode(code);
        task.setName(name);
        
        dao.save(task);
        return task;
    }
    
    public Task getByCode(String code) {
        DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass());
        criteria.add(Restrictions.eq("code", code));
        
        List<Task> tasks = dao.findByCriteria(criteria, 0, 1);
        return tasks.size()>0 ? tasks.get(0) : null;
    }
}
